package com.framework.seedkit.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.framework.seedkit.R;
import com.framework.seedkit.base.BaseFragmentActivity;
import com.framework.seedkit.ui.fragment.TestFrag1;
import com.framework.seedkit.ui.fragment.TestFrag2;
import com.framework.seedkit.ui.fragment.TestFrag3;
import com.framework.seedkit.ui.fragment.TestFrag4;

import butterknife.BindView;

public class FragInFragAct extends BaseFragmentActivity {
	@BindView(R.id.main_tab_rg) RadioGroup main_tab_rg;

	private FragmentManager fragmentManager;
	private FragmentTransaction transaction;
	
	// 缓存Fragment,用于控制隐藏Fragment
	public Fragment cacheFrag = null;

	public TestFrag1 testFrag1 = null;
	public TestFrag2 testFrag2 = null;
	public TestFrag3 testFrag3 = null;
	public TestFrag4 testFrag4 = null;

	private boolean bExit = false;
	
	private static boolean isGoToJsd = false;
	
	public static boolean isLoginStatusChange = false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		cusInit();
		
		findViewById(R.id.main_tab_home_btn).performClick();	
		
	}

	protected int getLayoutId() {
		return R.layout.act_frag_in_frag;
	}

	/** 组件初始化 */
	private void cusInit() {
		fragmentManager = getSupportFragmentManager();

		main_tab_rg = (RadioGroup) findViewById(R.id.main_tab_rg);

		main_tab_rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				transaction = fragmentManager.beginTransaction();

				setTabSelected(checkedId);
			}
		});

		((RadioButton)(main_tab_rg.getChildAt(0))).setChecked(true);
		
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	/** 设置默认选项卡 */
	private void setTabSelected(int checkedId) {
		if (null != cacheFrag) {
			transaction.hide(cacheFrag);
		}

		switch (checkedId) {
		case R.id.main_tab_home_btn:
			if (null == testFrag2) {
				testFrag2 = new TestFrag2();
				
				transaction.add(R.id.content, testFrag2,"TAB_MAIN");
			} else {
				transaction.show(testFrag2);
			}
			
			cacheFrag = testFrag2;

			break;
		case R.id.main_tab_bank_btn :
			
			if(testFrag3 == null) {
				testFrag3 = new TestFrag3();
				transaction.add(R.id.content, testFrag3,"TAB_CLUE");
			} else {
				transaction.show(testFrag3);
			}
			
			cacheFrag = testFrag3;

			break;
		case R.id.main_tab_assist_btn :
			
			if(testFrag3 == null) {
				testFrag3 = new TestFrag3();
				transaction.add(R.id.content, testFrag3,"TAB_ACCEPT");
			} else {
				transaction.show(testFrag3);
			}
			
			cacheFrag = testFrag3;

			break;
		case R.id.main_tab_user_btn:
			if (null == testFrag4) {
				testFrag4 = new TestFrag4();
				transaction.add(R.id.content, testFrag4, "TAB_FOLLOW");
			} else {
				transaction.show(testFrag4);

			}

			cacheFrag = testFrag4;

			break;

		default:
			if (null == testFrag1) {
				testFrag1 = new TestFrag1();
				transaction.add(R.id.content, testFrag1, "TAB_MAIN");
			} else {
				transaction.show(testFrag1);
			}

			cacheFrag = testFrag1;

			break;
		}

		transaction.commit();
	}

//	@Override
//	public void onBackPressed() {
//		if (bExit) {
//			AppActivityManager.getAppManager().AppExit(mContext);
////			finish();
//		} else {
//			bExit = true;
//			Toast.makeText(mContext, R.string.msg_app_exit_text, Toast.LENGTH_LONG).show();
//			new Handler().postDelayed(new Runnable() {
//				@Override
//				public void run() {
//					bExit = false;
//				}
//
//			}, 2 * 1000);
//		}
//	}
	
	@Override
	protected void onResume() {
		super.onResume();

		loginStatusChangeUpdateFragment();

	}
	
	private void loginStatusChangeUpdateFragment() {
		if(cacheFrag == testFrag1) {
			testFrag1.onHiddenChanged(true);
		} else if(cacheFrag == testFrag2) {
			testFrag2.onHiddenChanged(true);
		} else if(cacheFrag == testFrag3) {
			testFrag3.onHiddenChanged(true);
		} else if(cacheFrag == testFrag4) {
			testFrag4.onHiddenChanged(true);
		}
		isLoginStatusChange = false;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);

		if(cacheFrag != null) {
			cacheFrag.onActivityResult(requestCode, resultCode, data);
		}
	}
}
